package com.ease.framework.flex.controls {
   import flash.display.BitmapData;
   import flash.display.BitmapDataChannel;
   import flash.geom.Matrix;
   import flash.geom.Point;

   import mx.containers.Canvas;
   import mx.controls.Label;
   import mx.core.UIComponent;

   public class ImagedLabel extends Canvas {
      private var _text:String;
      private var _label:Label;
      private var _mask:UIComponent;
      private var _rotationPivots:Point; //旋转轴坐标，相对于imagelabel本身，仅在使用rotationAroundPivots属性进行旋转时有效
      private var _smooth:Boolean = true;

      public function set text(t:String):void {
         this._text = t;
         this._label.text = t;
         invalidateProperties();
         invalidateDisplayList();
         validateNow();
      }

      public function get text():String {
         return this._text;
      }

      public function set rotationPivots(point:Point):void {
         this._rotationPivots = point;
         originX = point.x;
         originY = point.y;
         invalidateProperties();
         invalidateDisplayList();
      }

      public function get rotationPivots():Point {
         return this._rotationPivots;
      }

      public function set smooth(issmooth:Boolean):void {
         this._smooth = issmooth;
         invalidateProperties();
         invalidateDisplayList();
      }

      public function get smooth():Boolean {
         return this._smooth;
      }

      public function ImagedLabel() {
         super();
         this._label = new Label();
         this._label.text = this.text;
         this.addChild(this._label);
         this._mask = new UIComponent();
         this.addChild(this._mask);
      }

      public function getTextWidth():Number {
         return this._label.width;
      }

      public function getTextHeight():Number {
         return this._label.height;
      }

      protected override function createChildren():void {
         super.createChildren();

      }

      protected override function commitProperties():void {
         super.commitProperties();
      }

      protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         graphics.clear();
         _mask.graphics.clear();

         var bd:BitmapData = new BitmapData(this._label.width, this._label.height, true, BitmapDataChannel.ALPHA);
         bd.draw(this._label);
         _mask.graphics.beginBitmapFill(bd);
         _mask.graphics.drawRect(0, 0, width, height);
         _mask.graphics.endFill();

      }

      private var originX:Number = 0; //自定义旋转轴的原始值
      private var originY:Number = 0;
      private var centerX:Number; //默认的旋转轴坐标，初始是0,0，但在位移后会发生变化
      private var centerY:Number;
      private var newX:Number;
      private var newY:Number;
      private var originalOffsetX:Number; //自定义旋转轴 在旋转后的新值
      private var originalOffsetY:Number;

      /**
       * 算法是，自定义旋转轴坐标以默认旋转轴为中心，旋转rotation度后，会得到一个新的旋转轴坐标，再将对象按这个坐标与默认原点反向移动
       * */
      public override function set rotation(value:Number):void {
         var radVal:Number = Math.PI * this.rotation / 180;

         if (isNaN(originX))
            originX = this.width / 2;

         if (isNaN(originY))
            originY = this.height / 2;

         centerX = this.x + originX * Math.cos(radVal) - originY * Math.sin(radVal);
         centerY = this.y + originX * Math.sin(radVal) + originY * Math.cos(radVal);

         super.rotation = value;

         radVal = Math.PI * value / 180;

         //旋转后，新的旋转中心点位置
         originalOffsetX = originX * Math.cos(radVal) - originY * Math.sin(radVal);
         originalOffsetY = originX * Math.sin(radVal) + originY * Math.cos(radVal);

         //将对象位移
         newX = Number((centerX - originalOffsetX).toFixed(1));
         newY = Number((centerY - originalOffsetY).toFixed(1));

         this.move(newX, newY);
      }
   }
}